﻿' TouchFlo Detacher
' Copyright (C) 2009, Avian Waves Software
' SEE LICENSE.TXT for software licensing information regarding original TouchFlo Detacher source code and binaries
' SEE OpenNetCF_LICENSE.TXT for licensing information regarding OpenNetCF binaries

Imports Microsoft.Win32

Public Class TFDRegistrySettings
  ' TFD's base registry key that all the settings TFD values are stored in (this value is fixed!)
  Public Shared TFDBaseKey As String = "Software\Avian Waves\TouchFlo Detacher"

#Region " Base Registry Key Settings "

  Public Shared Property TodaySoftKeyBaseKey() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "TodaySoftKeyBaseKey", "Software\Microsoft\Today\Keys")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "TodaySoftKeyBaseKey", value)
    End Set
  End Property

  Public Shared Property ManilaSoftKeyBaseKey() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "ManilaSoftKeyBaseKey", "Software\HTC\Manila")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "ManilaSoftKeyBaseKey", value)
    End Set
  End Property

  Public Shared Property TodayItemsBaseKey() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "TodayItemsBaseKey", "Software\Microsoft\Today\Items")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "TodayItemsBaseKey", value)
    End Set
  End Property

  Public Shared Property Manila3DTodayKey() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "Manila3DTodayKey", "TouchFLO 3D")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "Manila3DTodayKey", value)
    End Set
  End Property

  Public Shared Property Manila2DTodayKey() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "Manila2DTodayKey", "TouchFLO 2D")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "Manila2DTodayKey", value)
    End Set
  End Property
#End Region

#Region " TouchFlo Detacher Service Settings "

  Public Shared Property TouchFloMode() As Integer
    Get
      Return GetUserRegValueInt(TFDBaseKey, "TouchFloMode", 0)
    End Get
    Set(ByVal value As Integer)
      SetUserRegValueInt(TFDBaseKey, "TouchFloMode", value)
    End Set
  End Property

  Public Shared Property LaunchTouchFloOnStartup() As Boolean
    Get
      Return GetUserRegValueBool(TFDBaseKey, "LaunchTF3DOnStartup", True)
    End Get
    Set(ByVal value As Boolean)
      SetUserRegValueBool(TFDBaseKey, "LaunchTF3DOnStartup", value)
    End Set
  End Property

  Public Shared Property LaunchTouchFloOnStartupDelayInSeconds() As Integer
    Get
      Return GetUserRegValueInt(TFDBaseKey, "LaunchTouchFloOnStartupDelayInSeconds", 2)
    End Get
    Set(ByVal value As Integer)
      SetUserRegValueInt(TFDBaseKey, "LaunchTouchFloOnStartupDelayInSeconds", value)
    End Set
  End Property

  Public Shared Property LaunchAppAfterTouchFlo() As Boolean
    Get
      Return GetUserRegValueBool(TFDBaseKey, "LaunchAppAfterTouchFlo", False)
    End Get
    Set(ByVal value As Boolean)
      SetUserRegValueBool(TFDBaseKey, "LaunchAppAfterTouchFlo", value)
    End Set
  End Property

  Public Shared Property LaunchAppAfterTouchFloDelayInSeconds() As Integer
    Get
      Return GetUserRegValueInt(TFDBaseKey, "LaunchAppAfterTouchFloDelayInSeconds", 2)
    End Get
    Set(ByVal value As Integer)
      SetUserRegValueInt(TFDBaseKey, "LaunchAppAfterTouchFloDelayInSeconds", value)
    End Set
  End Property

  Public Shared Property LaunchAppAfterTouchFloPath() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "LaunchAppAfterTouchFloPath", "")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "LaunchAppAfterTouchFloPath", value)
    End Set
  End Property

  Public Shared Property LaunchTouchFloDialerWorkaround() As Boolean
    Get
      Return GetUserRegValueBool(TFDBaseKey, "LaunchTouchFloDialerWorkaround", False)
    End Get
    Set(ByVal value As Boolean)
      SetUserRegValueBool(TFDBaseKey, "LaunchTouchFloDialerWorkaround", value)
    End Set
  End Property

  Public Shared Property ShowServiceErrors() As Boolean
    Get
      Return GetUserRegValueBool(TFDBaseKey, "ShowServiceErrors", True)
    End Get
    Set(ByVal value As Boolean)
      SetUserRegValueBool(TFDBaseKey, "ShowServiceErrors", value)
    End Set
  End Property

  Public Shared Property TouchFloMainWindowClass() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "TouchFloMainWindowClass", "Manila")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "TouchFloMainWindowClass", value)
    End Set
  End Property

  Public Shared Property TouchFloSoftKeyWindowClass() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "TouchFloSoftKeyWindowClass", "menu_worker")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "TouchFloSoftKeyWindowClass", value)
    End Set
  End Property

  Public Shared Property TouchFloEXEOnly() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "TouchFloEXEOnly", "manila.exe")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "TouchFloEXEOnly", value)
    End Set
  End Property

  Public Shared Property TouchFloFullPath() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "TouchFloFullPath", "\windows\manila.exe")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "TouchFloFullPath", value)
    End Set
  End Property

#End Region

#Region " TouchFlo 3D Today Settings "

  Public Shared Property TF3DTodayEnabled() As Boolean
    Get
      Dim keys() As String = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(TodayItemsBaseKey, False).GetSubKeyNames
      If keys IsNot Nothing Then
        For Each s As String In keys
          If s.ToLower = Manila3DTodayKey.ToLower Then
            Return GetMachineRegValueBool(TodayItemsBaseKey & "\" & Manila3DTodayKey, "Enabled", False)
          End If
        Next
      End If

      Return False
    End Get
    Set(ByVal value As Boolean)
      SetMachineRegValueBool(TodayItemsBaseKey & "\" & Manila3DTodayKey, "Enabled", value)
    End Set
  End Property

  Public Shared Property TF2DTodayEnabled() As Boolean
    Get
      Dim keys() As String = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(TodayItemsBaseKey, False).GetSubKeyNames
      If keys IsNot Nothing Then
        For Each s As String In keys
          If s.ToLower = Manila2DTodayKey.ToLower Then
            Return GetMachineRegValueBool(TodayItemsBaseKey & "\" & Manila2DTodayKey, "Enabled", False)
          End If
        Next
      End If

      Return False
    End Get
    Set(ByVal value As Boolean)
      SetMachineRegValueBool(TodayItemsBaseKey & "\" & Manila2DTodayKey, "Enabled", value)
    End Set
  End Property

#End Region

#Region " TouchFlo 3D Softkeys "

  Public Shared Property TF3DLeftSoftKeyCmdValue() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "TF3DLeftSoftKeyCmdValue", "HomeLSKPath")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "TF3DLeftSoftKeyCmdValue", value)
    End Set
  End Property

  Public Shared Property TF3DLeftSoftKeyNameValue() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "TF3DLeftSoftKeyNameValue", "HomeLSKText")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "TF3DLeftSoftKeyNameValue", value)
    End Set
  End Property

  Public Shared Property TF3DRightSoftKeyCmdValue() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "TF3DRightSoftKeyCmdValue", "HomeRSKPath")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "TF3DRightSoftKeyCmdValue", value)
    End Set
  End Property

  Public Shared Property TF3DRightSoftKeyNameValue() As String
    Get
      Return GetUserRegValueString(TFDBaseKey, "TF3DRightSoftKeyNameValue", "HomeRSKText")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TFDBaseKey, "TF3DRightSoftKeyNameValue", value)
    End Set
  End Property

  Public Shared Property TF3DLeftSoftKeyApp() As String
    Get
      Return GetMachineRegValueString(ManilaSoftKeyBaseKey, TF3DLeftSoftKeyCmdValue, "")
    End Get
    Set(ByVal value As String)
      SetMachineRegValueString(ManilaSoftKeyBaseKey, TF3DLeftSoftKeyCmdValue, value)
    End Set
  End Property

  Public Shared Property TF3DLeftSoftKeyText() As String
    Get
      Return GetMachineRegValueString(ManilaSoftKeyBaseKey, TF3DLeftSoftKeyNameValue, "")
    End Get
    Set(ByVal value As String)
      SetMachineRegValueString(ManilaSoftKeyBaseKey, TF3DLeftSoftKeyNameValue, value)
    End Set
  End Property

  Public Shared Property TF3DRightSoftKeyApp() As String
    Get
      Return GetMachineRegValueString(ManilaSoftKeyBaseKey, TF3DRightSoftKeyCmdValue, "")
    End Get
    Set(ByVal value As String)
      SetMachineRegValueString(ManilaSoftKeyBaseKey, TF3DRightSoftKeyCmdValue, value)
    End Set
  End Property

  Public Shared Property TF3DRightSoftKeyText() As String
    Get
      Return GetMachineRegValueString(ManilaSoftKeyBaseKey, TF3DRightSoftKeyNameValue, "")
    End Get
    Set(ByVal value As String)
      SetMachineRegValueString(ManilaSoftKeyBaseKey, TF3DRightSoftKeyNameValue, value)
    End Set
  End Property

#End Region

#Region " Today Softkeys "

  Public Shared Property TodayLeftSoftKeyApp() As String
    Get
      Return GetUserRegValueString(TodaySoftKeyBaseKey & "\112", "Open", "")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TodaySoftKeyBaseKey & "\112", "Open", value)
    End Set
  End Property

  Public Shared Property TodayLeftSoftKeyText() As String
    Get
      Return GetUserRegValueString(TodaySoftKeyBaseKey & "\112", Nothing, "")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TodaySoftKeyBaseKey & "\112", Nothing, value)
    End Set
  End Property

  Public Shared Property TodayRightSoftKeyApp() As String
    Get
      Return GetUserRegValueString(TodaySoftKeyBaseKey & "\113", "Open", "")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TodaySoftKeyBaseKey & "\113", "Open", value)
    End Set
  End Property

  Public Shared Property TodayRightSoftKeyText() As String
    Get
      Return GetUserRegValueString(TodaySoftKeyBaseKey & "\113", Nothing, "")
    End Get
    Set(ByVal value As String)
      SetUserRegValueString(TodaySoftKeyBaseKey & "\113", Nothing, value)
    End Set
  End Property
#End Region

#Region " Registry helper methods "
  Public Shared Function GetUserRegValueBool(ByVal key As String, ByVal ValueName As String, ByVal DefaultValue As Boolean) As Boolean
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(key)
        If .GetValue(ValueName) IsNot Nothing Then
          Try
            Dim x As Integer = Integer.Parse(.GetValue(ValueName).ToString)
            If x = 0 Then
              Return False
            ElseIf x <> 1 Then
              .SetValue(ValueName, 1, Microsoft.Win32.RegistryValueKind.DWord)
            End If
            Return True
          Catch
          End Try
        End If

        If DefaultValue Then
          .SetValue(ValueName, 1, Microsoft.Win32.RegistryValueKind.DWord)
        Else
          .SetValue(ValueName, 0, Microsoft.Win32.RegistryValueKind.DWord)
        End If
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [GET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try

    Return DefaultValue
  End Function

  Public Shared Sub SetUserRegValueBool(ByVal key As String, ByVal ValueName As String, ByVal Value As Boolean)
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(key)
        If Value Then
          .SetValue(ValueName, 1, Microsoft.Win32.RegistryValueKind.DWord)
        Else
          .SetValue(ValueName, 0, Microsoft.Win32.RegistryValueKind.DWord)
        End If
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [SET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try
  End Sub

  Public Shared Function GetUserRegValueInt(ByVal key As String, ByVal ValueName As String, ByVal DefaultValue As Integer) As Integer
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(key)
        If .GetValue(ValueName) IsNot Nothing Then
          Try
            Dim x As Integer = Integer.Parse(.GetValue(ValueName).ToString)
            Return x
          Catch
          End Try
        End If

        .SetValue(ValueName, DefaultValue, Microsoft.Win32.RegistryValueKind.DWord)
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [GET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try

    Return DefaultValue
  End Function

  Public Shared Sub SetUserRegValueInt(ByVal key As String, ByVal ValueName As String, ByVal Value As Integer)
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(key)
        .SetValue(ValueName, Value, Microsoft.Win32.RegistryValueKind.DWord)
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [SET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try
  End Sub

  Public Shared Function GetUserRegValueString(ByVal key As String, ByVal ValueName As String, ByVal DefaultValue As String) As String
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(key)
        If .GetValue(ValueName) IsNot Nothing Then
          Try
            Dim x As String = .GetValue(ValueName).ToString
            Return x
          Catch
          End Try
        End If

        .SetValue(ValueName, DefaultValue, Microsoft.Win32.RegistryValueKind.String)
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [GET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try

    Return DefaultValue
  End Function

  Public Shared Sub SetUserRegValueString(ByVal key As String, ByVal ValueName As String, ByVal Value As String)
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(key)
        .SetValue(ValueName, Value, Microsoft.Win32.RegistryValueKind.String)
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [SET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try
  End Sub





  Public Shared Function GetMachineRegValueBool(ByVal key As String, ByVal ValueName As String, ByVal DefaultValue As Boolean) As Boolean
    Try
      With Microsoft.Win32.Registry.LocalMachine.CreateSubKey(key)
        If .GetValue(ValueName) IsNot Nothing Then
          Try
            Dim x As Integer = Integer.Parse(.GetValue(ValueName).ToString)
            If x = 0 Then
              Return False
            ElseIf x <> 1 Then
              .SetValue(ValueName, 1, Microsoft.Win32.RegistryValueKind.DWord)
            End If
            Return True
          Catch
          End Try
        End If

        If DefaultValue Then
          .SetValue(ValueName, 1, Microsoft.Win32.RegistryValueKind.DWord)
        Else
          .SetValue(ValueName, 0, Microsoft.Win32.RegistryValueKind.DWord)
        End If
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [GET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try

    Return DefaultValue
  End Function

  Public Shared Sub SetMachineRegValueBool(ByVal key As String, ByVal ValueName As String, ByVal Value As Boolean)
    Try
      With Microsoft.Win32.Registry.LocalMachine.CreateSubKey(key)
        If Value Then
          .SetValue(ValueName, 1, Microsoft.Win32.RegistryValueKind.DWord)
        Else
          .SetValue(ValueName, 0, Microsoft.Win32.RegistryValueKind.DWord)
        End If
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [SET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try
  End Sub

  Public Shared Function GetMachineRegValueInt(ByVal key As String, ByVal ValueName As String, ByVal DefaultValue As Integer) As Integer
    Try
      With Microsoft.Win32.Registry.LocalMachine.CreateSubKey(key)
        If .GetValue(ValueName) IsNot Nothing Then
          Try
            Dim x As Integer = Integer.Parse(.GetValue(ValueName).ToString)
            Return x
          Catch
          End Try
        End If

        .SetValue(ValueName, DefaultValue, Microsoft.Win32.RegistryValueKind.DWord)
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [GET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try

    Return DefaultValue
  End Function

  Public Shared Sub SetMachineRegValueInt(ByVal key As String, ByVal ValueName As String, ByVal Value As Integer)
    Try
      With Microsoft.Win32.Registry.LocalMachine.CreateSubKey(key)
        .SetValue(ValueName, Value, Microsoft.Win32.RegistryValueKind.DWord)
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [SET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try
  End Sub

  Public Shared Function GetMachineRegValueString(ByVal key As String, ByVal ValueName As String, ByVal DefaultValue As String) As String
    Try
      With Microsoft.Win32.Registry.LocalMachine.CreateSubKey(key)
        If .GetValue(ValueName) IsNot Nothing Then
          Try
            Dim x As String = .GetValue(ValueName).ToString
            Return x
          Catch
          End Try
        End If

        .SetValue(ValueName, DefaultValue, Microsoft.Win32.RegistryValueKind.String)
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [GET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try

    Return DefaultValue
  End Function

  Public Shared Sub SetMachineRegValueString(ByVal key As String, ByVal ValueName As String, ByVal Value As String)
    Try
      With Microsoft.Win32.Registry.LocalMachine.CreateSubKey(key)
        .SetValue(ValueName, Value, Microsoft.Win32.RegistryValueKind.String)
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [SET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try
  End Sub
#End Region

End Class
